package com.example.messagemodule.entity;


import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.examination.mlib.constants.UserInfoConstants;
import com.hyphenate.chat.EMCustomMessageBody;
import com.hyphenate.chat.EMMessage;
import com.yilijk.base.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class ConversationMessage implements MultiItemEntity {

    public EMMessage emMessage;
    public String seftAvator;
    public String fromAvator;


    public ConversationMessage(EMMessage emMessage, String seftAvator, String fromAvator) {
        this.emMessage = emMessage;
        this.seftAvator = seftAvator;
        this.fromAvator = fromAvator;
    }




    public EMMessage getEmMessage() {
        return emMessage;
    }

    public void setEmMessage(EMMessage emMessage) {
        this.emMessage = emMessage;
    }

    public String getSeftAvator() {
        return seftAvator;
    }

    public void setSeftAvator(String seftAvator) {
        this.seftAvator = seftAvator;
    }

    public String getFromAvator() {
        return fromAvator;
    }

    public void setFromAvator(String fromAvator) {
        this.fromAvator = fromAvator;
    }

    @Override
    public int getItemType() {
        int messageType = 0;
        int isMyMessage = 0;
        if (this.emMessage.getType() == EMMessage.Type.TXT) {
            String string = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.imAccount, "");
            Map<String, Object> ext = this.emMessage.ext();
            JSONObject templateExt =null;
            if (ext.containsKey("templateExt")){
                String templateExtstr = (String) ext.get("templateExt");
                try {
                    templateExt = new JSONObject(templateExtstr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            try {
                if (!this.emMessage.getFrom().equals(string) && ext!=null && templateExt!=null && templateExt.has("first") && (templateExt.getBoolean("first")|| "true".equals(templateExt.getString("first"))) ){
                    messageType = 11;
                }else{
                    messageType = 1;
                }
            } catch (JSONException e) {
                e.printStackTrace();
                messageType = 1;
            }


        } else if (this.emMessage.getType() == EMMessage.Type.IMAGE) {
            messageType = 2;
        } else if (this.emMessage.getType() == EMMessage.Type.VIDEO) {
            messageType = 3;
        } else if (this.emMessage.getType() == EMMessage.Type.LOCATION) {
            messageType = 4;
        } else if (this.emMessage.getType() == EMMessage.Type.VOICE) {
            messageType = 5;
        } else if (this.emMessage.getType() == EMMessage.Type.FILE) {
            messageType = 6;
        } else if (this.emMessage.getType() == EMMessage.Type.CMD) {
            messageType = 7;
        } else if (this.emMessage.getType() == EMMessage.Type.CUSTOM) {
            EMCustomMessageBody customMessageBody = (EMCustomMessageBody) this.emMessage.getBody();
            if (customMessageBody.event().equals("pharmacist_audit")) {
//药师审核处方，展示处方卡片
//                    messageType = 801;
            } else if (customMessageBody.event().equals("to_evaluate")) {
//请对我的服务作出评价
                messageType = 802;
            } else if (customMessageBody.event().equals("chargesList")) {
//问诊单

                Map<String, String> params = customMessageBody.getParams();
                if( params!=null && params.containsKey("type") && (Integer.parseInt(params.get("type")) == 0 ||Integer.parseInt(params.get("type")) == 2 || Integer.parseInt(params.get("type")) == 4)){
                    messageType = 8003;
                }else{
                    messageType = 803;
                }
            } else if (customMessageBody.event().equals("patient_evaluate_finish")) {
// 患者评价完医生
                messageType = 804;
            }  else if (customMessageBody.event().equals("received")) {
// 医生接诊
                messageType = 805;
            } else if (customMessageBody.event().equals("finish")) {
// 结束问诊
                messageType = 806;
            }else if (customMessageBody.event().equals("stop_send_template")) {
// 问诊模板结束
                messageType = 807;
            } else if (customMessageBody.event().equals("doctor_prescribing")) {
//温馨提示：医生已开具处方，药师正在审核中，审核通过后，您可以根据提示在线购买药品。
                messageType = 808;
            } else if (customMessageBody.event().equals("doctor_evaluate_thanks")) {
// 医生感谢
            } else if (customMessageBody.event().equals("doctor_edit_prescribing")) {
// 医生修改处方
            } else if (customMessageBody.event().equals("cancel")) {
// 系统超时取消
            }else if (customMessageBody.event().equals("delay_charges")) {
//温馨提示：医生为您延长{{item.customExts.duration}}分钟问诊时间，您可以继续向医生进行提问。
                messageType = 901;
            } else if (customMessageBody.event().equals("refund")) {
// 医生退诊
                messageType = 904;
            } else if (customMessageBody.event().equals("last_charges_template")) {
// 最后一个问诊模板消息
                messageType = 906;
            } else if (customMessageBody.event().equals("pharmacist_audit_fail")) {
// 药师审核处方，展示处方卡片
                messageType = 907;
            }else if (customMessageBody.event().equals("patient_update_charges")){
                // 患者更新问诊单信息
                messageType = 909;
            }else if (customMessageBody.event().equals("recall")){
                messageType = 908;
            } else if (customMessageBody.event().equals("setting_phone_time")) {
// 设置电话时间
//                messageType = 909;
            } else if (customMessageBody.event().equals("phone_call_end")) {
// 结束通话
//                messageType = 910;
            } else if (customMessageBody.event().equals("miss_phone_call")) {
// 未接电话
//                messageType = 911;
            }
        }
        if (this.emMessage.getFrom().equals(SharedPreferencesUtils.getInstance().getString(UserInfoConstants.imAccount, ""))) {
            isMyMessage = 1;
        } else {
            isMyMessage = 0;
        }
        int type = Integer.parseInt(messageType + "" + isMyMessage);
        return type;
    }
}
